package com.cqs.leetcode.array;

import com.cqs.leetcode.ds.ArraysUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * lixw
 * 2021/11/4
 **/
public class QueueReconstruction406 {

    public int[][] reconstructQueue(int[][] people) {
        Arrays.sort(people, (o1, o2) -> {
            //高度倒序
            if (o2[0] != o1[0]) {
                return o2[0] - o1[0];
            }
            //第二个属性升序
            return o1[1] - o2[1];
        });

        List<int[]> list = new ArrayList<>();
        for (int i = 0; i < people.length; i++) {
            int[] p = people[i];
            list.add(p[1], p);
        }
        int[][] result = new int[people.length][2];
        for (int i = 0; i < people.length; i++) {
            result[i] = list.get(i);
        }
        return result;
    }

    public static void main(String[] args) {
        QueueReconstruction406 q = new QueueReconstruction406();
        List<int[][]> cases = new ArrayList<>();
        cases.add(new int[][]{{7, 0}});
        cases.add(new int[][]{{7, 0},{6,0}});
        cases.add(new int[][]{{7, 0}, {4, 4}, {7, 1}, {5, 0}, {6, 1}, {5, 2}});
        cases.add(new int[][]{{5, 0}, {7, 0}, {5, 2}, {6, 1}, {4, 4}, {7, 1}});
        cases.add(new int[][]{{6, 0}, {5, 0}, {4, 0}, {3, 2}, {2, 2}, {1, 4}});
        for (int[][] c : cases) {
            int[][] val = q.reconstructQueue(c);
            System.out.println(ArraysUtils.formatInts(c) + "----" + ArraysUtils.formatInts(val));
        }
    }
}
